package com.naiterui.ehp.bs.cms.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bp.bo.b2c.order.ReturnOrderBO;
import com.naiterui.ehp.bp.bo.b2c.order.ReturnOrderItemBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.oms.entity.ReturnOrder;
import com.naiterui.ehp.bs.cms.modules.oms.feign.IReturnOrderFeginClient;
import com.naiterui.ehp.bs.cms.modules.oms.mapper.OrderMapper;
import com.naiterui.ehp.bs.cms.modules.oms.mapper.ReturnOrderItemMapper;
import com.naiterui.ehp.bs.cms.modules.oms.mapper.ReturnOrderMapper;
import com.naiterui.ehp.bs.cms.modules.oms.mapper.ReturnOrderOperateLogMapper;
import com.naiterui.ehp.bs.cms.modules.oms.service.IReturnOrderService;
import com.naiterui.ehp.bs.cms.modules.oms.vo.BaseOrderInfoVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.ExchangeOrderDetailVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.ExchangeOrderVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.LogisticsCompanyVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.OperateLogVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.ReturnOrderDetailVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.ReturnOrderItemVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.ReturnOrderParamVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.ReturnOrderVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.SupplementaryOrderDetailVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.SupplementaryOrderVO;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Slf4j
@Service
public class ReturnOrderServiceImpl extends ServiceImpl<ReturnOrderMapper, ReturnOrder> implements IReturnOrderService {

  @Autowired
  private ReturnOrderMapper returnGoodsOrderMapper;
  @Autowired
  private ReturnOrderItemMapper returnGoodsItemMapper;
  @Autowired
  private ReturnOrderOperateLogMapper returnGoodsOperateLogMapper;
  @Autowired
  private OrderMapper orderMapper;
  @Autowired
  private IReturnOrderFeginClient returnOrderFeginClient;

  @Override
  public PageVO<ReturnOrderDetailVO> queryPage(PageParamsVO pageParamsVO, ReturnOrderParamVO paramVO) {
    QueryWrapper<ReturnOrderDetailVO> entityWrapper = pageParamsVO.getEntityWrapper("t");
    //创建时间
    String startTime = paramVO.getStartTime();
    String endTime = paramVO.getEndTime();
    entityWrapper.between(startTime != null && endTime != null, "t.created_at", startTime, endTime);
    String orderSn = paramVO.getOrderSn();
    entityWrapper.eq(StringUtils.isNotBlank(orderSn), "t.order_sn", orderSn);
    String businessNo = paramVO.getBusinessNo();
    entityWrapper.eq(StringUtils.isNotBlank(businessNo), "t.business_no", businessNo);
    String phone = paramVO.getPhone();
    entityWrapper.eq(StringUtils.isNotBlank(phone), "ou.phone", phone);
    String userName = paramVO.getReceiver();
    entityWrapper.eq(StringUtils.isNotBlank(userName), "ou.receiver", userName);
    Integer type = paramVO.getType();
    entityWrapper.eq(type != null, "t.type", type);
    Integer status = paramVO.getStatus();
    entityWrapper.eq(status != null, "t.status", status);

    Page<ReturnOrderDetailVO> page = this.baseMapper.getPage(pageParamsVO.getPage(), entityWrapper);
    return PageVO.toPageVo(page);
  }

  @Override
  public ReturnOrderDetailVO getReturnOrderDetail(Long id) {
    //查询退货单信息
    ReturnOrderDetailVO returnOrder = this.returnGoodsOrderMapper.getById(id);
    if (returnOrder != null) {
      //查询退货关联订单信息
      BaseOrderInfoVO orderInfoVO = this.orderMapper.getByOrderSn(returnOrder.getOrderSn());
      returnOrder.setOrderInfoVO(orderInfoVO);

      //查询退货列表
      List<ReturnOrderItemVO> products = this.returnGoodsItemMapper.findByBusinessNO(returnOrder.getBusinessNo());
      returnOrder.setReturnProducts(products);

      //查询该退货单操作日志列表
      List<OperateLogVO> operateLogs = this.returnGoodsOperateLogMapper.findByBusinessNO(returnOrder.getBusinessNo());
      returnOrder.setOperateLogs(operateLogs);
    }
    return returnOrder;
  }

  @Override
  public ExchangeOrderDetailVO getExchangeOrderDetail(Long id) {
    ReturnOrderDetailVO returnOrderVO = this.getReturnOrderDetail(id);
    if (returnOrderVO != null) {
      ExchangeOrderDetailVO exchangeOrderVO = new ExchangeOrderDetailVO();
      BeanUtils.copyProperties(returnOrderVO, exchangeOrderVO);
      //设置换货商品
      exchangeOrderVO.setExchanges(
          exchangeOrderVO
              .getReturnProducts()
              .stream()
              .filter(s -> ReturnOrderItemVO.TYPE_EXCHANGE == s.getType())
              .collect(Collectors.toList()));
      //设置退货商品
      exchangeOrderVO.setReturnProducts(
          exchangeOrderVO
              .getReturnProducts()
              .stream()
              .filter(s -> ReturnOrderItemVO.TYPE_RETURN == s.getType())
              .collect(Collectors.toList()));
      return exchangeOrderVO;
    }

    return null;
  }

  @Override
  public SupplementaryOrderDetailVO getSupplementaryOrderDetail(Long id) {
    //查询退货单信息
    ReturnOrderDetailVO returnOrder = this.getReturnOrderDetail(id);
    SupplementaryOrderDetailVO vo = new SupplementaryOrderDetailVO();
    BeanUtils.copyProperties(returnOrder, vo);
    //复制补寄商品信息
    vo.setExchanges(returnOrder.getReturnProducts());
    return vo;
  }

  @Override
  public void saveReturnOrder(ReturnOrderVO orderVO) throws BusinessException {

    ReturnOrderBO bo = new ReturnOrderBO();
    BeanUtils.copyProperties(orderVO, bo);
    List<ReturnOrderItemBO> returnItems = new ArrayList<>();
    Optional.ofNullable(orderVO.getReturnProducts()).ifPresent(list -> list.forEach(s -> {
      ReturnOrderItemBO itemBO = new ReturnOrderItemBO();
      BeanUtils.copyProperties(s, itemBO);
      itemBO.setType(ReturnOrderBO.TYPE_RETURN);
      returnItems.add(itemBO);
    }));
    bo.setReturnProducts(returnItems);
    bo.setType(ReturnOrderBO.TYPE_RETURN);

    this.returnOrderFeginClient.saveReturnOrder(bo);
  }

  @Override
  public void saveExchangeOrder(ExchangeOrderVO orderVO) throws BusinessException {
    ReturnOrderBO bo = new ReturnOrderBO();
    BeanUtils.copyProperties(orderVO, bo);
    List<ReturnOrderItemBO> returnItems = new ArrayList<>();
    Optional.ofNullable(orderVO.getReturnProducts()).ifPresent(list -> list.forEach(s -> {
      ReturnOrderItemBO itemBO = new ReturnOrderItemBO();
      BeanUtils.copyProperties(s, itemBO);
      itemBO.setType(ReturnOrderBO.TYPE_RETURN);
      returnItems.add(itemBO);
    }));
    bo.setReturnProducts(returnItems);
    List<ReturnOrderItemBO> exchanges = new ArrayList<>();
    Optional.ofNullable(orderVO.getExchanges()).ifPresent(list -> list.forEach(s -> {
      ReturnOrderItemBO itemBO = new ReturnOrderItemBO();
      BeanUtils.copyProperties(s, itemBO);
      itemBO.setType(ReturnOrderBO.TYPE_EXCHANGE);
      exchanges.add(itemBO);
    }));
    bo.setExchanges(exchanges);
    bo.setType(ReturnOrderBO.TYPE_EXCHANGE);
    this.returnOrderFeginClient.saveReturnOrder(bo);
  }

  @Override
  public void saveSupplementaryOrder(SupplementaryOrderVO orderVO) throws BusinessException {
    ReturnOrderBO bo = new ReturnOrderBO();
    BeanUtils.copyProperties(orderVO, bo);
    List<ReturnOrderItemBO> exchanges = new ArrayList<>();
    Optional.ofNullable(orderVO.getExchanges()).ifPresent(list -> list.forEach(s -> {
      ReturnOrderItemBO itemBO = new ReturnOrderItemBO();
      BeanUtils.copyProperties(s, itemBO);
      itemBO.setType(ReturnOrderBO.TYPE_SUPPLEMENTARY);
      exchanges.add(itemBO);
    }));
    bo.setExchanges(exchanges);
    bo.setType(ReturnOrderBO.TYPE_SUPPLEMENTARY);
    this.returnOrderFeginClient.saveReturnOrder(bo);
  }

  @Override
  public void audit(List<Long> ids) throws BusinessException {
    this.returnOrderFeginClient.audit(ids);
  }

  @Override
  public void reset(List<Long> ids) throws BusinessException {
    this.returnOrderFeginClient.reset(ids);
  }

  @Override
  public void submit(List<Long> ids) throws BusinessException {
    this.returnOrderFeginClient.submit(ids);
  }

  @Override
  public void invalid(List<Long> ids) throws BusinessException {
    this.returnOrderFeginClient.invalid(ids);
  }

  @Override
  public void confirm(List<Long> ids) throws BusinessException {
    this.returnOrderFeginClient.confirm(ids);
  }

  @Override
  public List<LogisticsCompanyVO> logisticsCompanyList() {
    return this.baseMapper.getLogisticsCompanyList();
  }

}
